<template>
	<view>
		<view class="u-skeleton">
			<view class="input-con">
				<view class="tit u-skeleton-fillet">输入充值金额</view>
				<input class="uni-input input u-skeleton-fillet" type="number" placeholder="请输入充值金额" placeholder-style="color:#ccc" v-model="price" />
				<view class="pay-btn u-skeleton-fillet" @click="pay">立即充值</view>
			</view>
			<view class="price">
				<view class="tit u-skeleton-fillet">充值满赠</view>
				<view class="specifications">
					<view class="item" v-for="(item, index) in priceList" :key="index" @click="paySel(item)">
						<view class="or-price"><text></text>{{ item.value }}</view>
						<view class="text u-skeleton-fillet">余额</view>
						<view class="cu-price u-skeleton-fillet">{{ item.key }}元</view>
					</view>
				</view>
				<view class="info">充值金额仅用于平台律师咨询消费，不支持激活私人律师尊享服务，充值后不支持提现。</view>
				
				<popup-pay :popShow="popShow" :money="money" @closePopup="closePopup" @confirmPay="confirmPay"></popup-pay>
			</view>
		</view>
		<u-skeleton :loading="loading" el-color="#eee" bg-color="#fff" :animation="true"></u-skeleton>
	</view>
</template>

<script>
	import { createBalanceOrder, payBalanceOrder, payBalanceGetSet } from '@/api/userApi.js'
	export default {
		data() {
			return {
				loading: true, // 是否显示骨架屏组件
				price: '',
				popShow: false,
				btnDisabled: false,
				money: {recharge: 1}, // 弹出的余额和价格
				priceList: [
					{
						id: 1,
						originalPrice: 28,
						currentPrice: 18
					},
					{
						id: 2,
						originalPrice: 238,
						currentPrice: 188
					},
					{
						id: 3,
						originalPrice: 328,
						currentPrice: 288
					}
				]
			}
		},
		onLoad() {
			this._payBalanceGetSet() // 获取充值配置
		},
		methods: {
			async _payBalanceGetSet() {
				const res = await payBalanceGetSet()
				this.priceList = res.data.data.top_up
				setTimeout(() => {
					this.loading = false
				}, 300)
				console.log(res)
			},
			async pay() {
				if (this.price <= 0) return this.$u.toast('请输入金额')
				this.money.price = this.price
				console.log(this.money)
				const res = await createBalanceOrder({balance: this.price-0})
				this.orderId = res.data.data.id
				console.log(res)
				this.popShow = true
			},
			async paySel(item) {
				console.log(item)
				this.money.price = item.key
				const res = await createBalanceOrder({balance: item.key-0})
				this.orderId = res.data.data.id
				console.log(res)
				this.popShow = true
			},
			closePopup(data) {
				console.log(data)
				this.popShow = data
				this.btnDisabled = false
			},
			async confirmPay(e) {
				console.log(e)
				this.popShow = e.popShow
				uni.showLoading({title: '加载中...'})
				const res = await payBalanceOrder({id: this.orderId-0, paytype: e.paymentWay})
				console.log(res)
				if (res.data.data.price == 0) return uni.navigateTo({url: '/pages/order/pay-success?orderId='+this.orderId})
				// #ifdef APP-PLUS
				if (e.paymentWay === 3) {
					// 微信支付
					this.$utils.appWxPay(res.data.data.appConfig, (success)=> {
						console.log(success)
						uni.setStorageSync('isRefresh', true)
						uni.switchTab({url: '/pages/user/user'})
					}, (error) => {
						console.log(error)
					})
				} else if (e.paymentWay === 2) {
					console.log('支付宝支付')
					uni.setStorageSync('isRefresh', true)
					uni.switchTab({url: '/pages/user/user'})
				} else if (e.paymentWay === 1) {
					console.log('余额支付')
					// uni.navigateTo({url: '/pages/order/pay-success?orderId=' + res.data.data.id})
				}
				// #endif
				
			}
 		}
	}
</script>

<style lang="scss">
	.input-con {
		height: 380rpx;
		background-image: linear-gradient(to right, #223977, #051a4c);
		padding: 0 30rpx;
		.tit {
			font-size: 34rpx;
			color: #ebecf0;
			padding-top: 30rpx;
			padding-left: 10rpx;
		}
		.input {
			height: 100rpx;
			background-color: #ebecf0;
			border-radius: 6rpx;
			padding-left: 25rpx;
			color: #1b1b1b;
			font-size: 40rpx;
			margin-top: 30rpx;
		}
		.pay-btn {
			height: 100rpx;
			background-color: #fcd5b8;
			border-radius: 6rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 36rpx;
			color: #674830;
			margin-top: 30rpx;
			&:active {
				opacity: .9;
			}
		}
	}
	.price {
		padding: 0 30rpx;
		.tit {
			font-size: 34rpx;
			color: #0d2647;
			padding: 50rpx 0 32rpx 12rpx;
		}
		.specifications {
			display: flex;
			justify-content: space-between;
			.item {
				width: 215rpx;
				height: 240rpx;
				background-color: #fff;
				border-radius: 6rpx;
				border: 2rpx solid #051b4d;
				position: relative;
				.or-price {
					text-align: center;
					font-size: 44rpx;
					color: #061a4e;
					text {
						font-size: 22rpx;
					}
					padding-top: 38rpx;
				}
				.text {
					color: #061a4e;
					text-align: center;
					font-size: 24rpx;
				}
				.cu-price {
					position: absolute;
					bottom: 0;
					left: 0;
					right: 0;
					height: 60rpx;
					font-size: 30rpx;
					color: #fcd5b8;
					display: flex;
					justify-content: center;
					align-items: center;
					background-image: linear-gradient(to right,#223876,#051a4d);
				}
				
			}
		}
		.info {
			font-size: 22rpx;
			color: #93969c;
			line-height: 40rpx;
			padding-top: 45rpx;
		}
	}
</style>
